package com.conan.service.Impl;

import com.conan.mapper.UserMapper;
import com.conan.pojo.User;
import com.conan.service.UserService;
import com.conan.utils.Md5Util;
import com.conan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void register(String username, String password) {
        //要先将密码通过MD5加密，然后调用Mapper层进行注册
        String md5Password = Md5Util.getMD5String(password);
        userMapper.insertUser(username, md5Password);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public void updateAvatar(String avatarURL) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarURL, id);
    }

    @Override
    public void updatePassword(String password) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePWD(Md5Util.getMD5String(password),id);
    }

}
